package com.pignest.hotel.ui.user;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.Composer;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import com.pignest.hotel.beans.Role;
import com.pignest.hotel.service.RoleService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;
import com.pignest.hotel.util.UIUtils;

@SuppressWarnings("unchecked")
public class RoleForm extends Grid implements Composer, EventListener {
	private static final long serialVersionUID = -7303164632788528900L;
	
	private RoleService roleService;
	private Role role;
	
	private Textbox nameTextbox;
	private Label captionLabel;
	private Component permissionContainer;
	private Textbox remarkTextbox;
	
	public RoleForm() {
		roleService = Services.getRoleService();
		addEventListener("onCreate", this);
	}
	
	
	
	public void setRole(Role role) {
		this.role = role;
		onRoleChanged();
	}
	
	private void onRoleChanged() {
		Role role = this.role;
		boolean edit = role != null;
		if (edit) {
			captionLabel.setValue("修改角色: " + role.getName());
			nameTextbox.setValue(role.getName());
			remarkTextbox.setValue(role.getDescription());
			List<Component> children = permissionContainer.getChildren();
			for (Component container : children) {
				for (Component component : (List<Component>)container.getChildren()) {
					if (!(component instanceof Checkbox))
						continue;
					Object o = component.getAttribute("permission");
					if (!(o instanceof String))
						continue;
//					Permission p = Permission.valueof((String)o);
//					if (p == null)
//						continue;
					((Checkbox)component).setChecked(role.hasPermission((String)o));
				}
			}
		} else {
			captionLabel.setValue("新建角色");
			nameTextbox.setValue(null);
			remarkTextbox.setValue(null);
			List<Component> children = permissionContainer.getChildren();
			for (Component container : children) {
				for (Component component : (List<Component>)container.getChildren()) {
					if (!(component instanceof Checkbox))
						continue;
					Object o = component.getAttribute("permission");
					if (!(o instanceof String))
						continue;
//					Permission p = Permission.valueof((String)o);
//					if (p == null)
//						continue;
					((Checkbox)component).setChecked(false);
				}
			}
		}
	}
	
	private List<String> getPermissions() {
		ArrayList<String> ps = new ArrayList<String>();
		List<Component> children = permissionContainer.getChildren();
		for (Component container : children) {
			for (Component component : (List<Component>)container.getChildren()) {
				if (!(component instanceof Checkbox))
					continue;
				if (!((Checkbox)component).isChecked())
					continue;
				Object o = component.getAttribute("permission");
				if (!(o instanceof String))
					continue;
//				Permission p = Permission.valueof((String)o);
//				if (p == null)
//					continue;
				ps.add((String)o);
			}
		}
		return ps;
	}
	
	public void onEdit(Component c) {
		c = c == null ? null : c.getParent();
		if (c == null)
			return;
		String roleId = (String)c.getAttribute("roleId");
		Role role = roleService.get(Role.class, roleId);
		if (role == null)
			return;
		setRole(role);
	}
	
	public void onDelete(Component c) {
		c = c == null ? null : c.getParent();
		if (c == null)
			return;
		String roleId = (String)c.getAttribute("roleId");
		Role role = roleService.get(Role.class, roleId);
		if (role == null)
			return;
		if (UIUtils.confirm("是否要删除角色:　" + role.getName())) {
			roleService.deleteRole(role);
			L.delete(role);
			UIUtils.refresh();
		}
	}

	
	public void onSave() {
		Role role = this.role;
		boolean edit = role != null;
		String name = nameTextbox.getValue();
		if (StringUtils.isEmpty(name)) {
			UIUtils.alert("角色名必填");
			return;
		}
		List<String> ps = getPermissions();
		if (edit) {
			if (!name.equals(role.getName())) {
				Role r = roleService.getRoleByName(name);
				if (r != null) {
					UIUtils.alert("角色名为: " + name + " 的角色已经存在!");
					return;
				}
				role.setName(name);
			}
			role.setDescription(remarkTextbox.getValue());
			role.setPermissions(ps);
			Services.getUserService().update(role);
			L.update(role);
//			this.role = null;
//			onRoleChanged();
		} else {
			role = new Role();
			role.setName(name);
			role.setDescription(remarkTextbox.getValue());
			role.setPermissions(ps);
			Services.getUserService().insert(role);
			L.insert(role);
		}
		UIUtils.alert("保存成功!");
		UIUtils.refresh();
	}
	
	public void onReset() {
		onRoleChanged();
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		
	}

	@Override
	public void onEvent(Event event) throws Exception {
		if ("onCreate".equals(event.getName())) {
			nameTextbox = (Textbox)getFellow("roleName");
			captionLabel = (Label)getFellow("captionLabel");
			permissionContainer = getFellow("permissionContainer");
			remarkTextbox = (Textbox)getFellow("roleRemark");
		}
	}

}
